﻿/**
 * File:   border_text.c
 * Author: 
 * Brief:  
 *
 * Copyright (c) 2024 - 2024 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * License file for more details.
 *
 */

/**
 * History:
 * ================================================================
 * 2024-10-20  created
 *
 */


#include "tkc/mem.h"
#include "tkc/utils.h"
#include "base/canvas.h"
#include "base/vgcanvas.h"
#include "base/canvas_offline.h"
#include "base/widget_vtable.h"
#include "base/style.h"
#include "tkc/color.h"
#include "tkc/rect.h"
#include "base/font_manager.h"
#include "border_text.h"


static ret_t border_text_get_prop(widget_t* widget, const char* name, value_t* v) {
  border_text_t* border_text = BORDER_TEXT(widget);
  return_value_if_fail(border_text != NULL && name != NULL && v != NULL, RET_BAD_PARAMS);

  if(tk_str_eq(name, WIDGET_PROP_TEXT)){
    value_set_wstr(v, widget->text.str);
    return RET_OK;
  }


  return RET_NOT_FOUND;
}

static ret_t border_text_set_prop(widget_t* widget, const char* name, const value_t* v) {
  border_text_t* border_text = BORDER_TEXT(widget);
  return_value_if_fail(widget != NULL && name != NULL && v != NULL, RET_BAD_PARAMS);

  if(tk_str_eq(name, WIDGET_PROP_TEXT)){
    wstr_from_value(&widget->text, v);
    return RET_OK;
  }


  return RET_NOT_FOUND;
}

static ret_t border_text_on_destroy(widget_t* widget) {
  border_text_t* border_text = BORDER_TEXT(widget);
  return_value_if_fail(widget != NULL && border_text != NULL, RET_BAD_PARAMS);



  return RET_OK;
}


static ret_t border_text_on_paint_self(widget_t* widget, canvas_t* c) {
  border_text_t* border_text = BORDER_TEXT(widget);
  style_t *style = widget->astyle;
  int font_size = style_get_int(style, STYLE_ID_FONT_SIZE, 25);
  const char *font_name = style_get_str(style, STYLE_ID_FONT_NAME, "default");
  int align_h = style_get_int(style, STYLE_ID_TEXT_ALIGN_H, ALIGN_H_LEFT);
  int align_v = style_get_int(style, STYLE_ID_TEXT_ALIGN_V, ALIGN_V_MIDDLE);

  color_t text_color = color_init(255, 255, 255, 255);  
  color_t border_color = color_init(0, 0, 0, 255);
  
  canvas_set_text_align(c, align_h, align_v);
  canvas_set_font(c, font_name, font_size);
  canvas_set_text_color(c, border_color);
  for(int j = 0; j < 3; j++){
    for(int i = 0; i < 3; i++){
      rect_t r = rect_init(i, j, widget->w, widget->h);
      canvas_draw_text_in_rect(c, widget->text.str, widget->text.size, &r);
    }
  }

  canvas_set_text_color(c, text_color);
  rect_t r = rect_init(1, 1, widget->w, widget->h);
  canvas_draw_text_in_rect(c, widget->text.str, widget->text.size, &r);
  return RET_OK;
}

static ret_t border_text_on_event(widget_t* widget, event_t* e) {
  border_text_t* border_text = BORDER_TEXT(widget);
  return_value_if_fail(widget != NULL && border_text != NULL, RET_BAD_PARAMS);

  (void)border_text;

  return RET_OK;
}

const char* s_border_text_properties[] = {
  NULL
};

TK_DECL_VTABLE(border_text) = {.size = sizeof(border_text_t),
                          .type = WIDGET_TYPE_BORDER_TEXT,
                          .clone_properties = s_border_text_properties,
                          .persistent_properties = s_border_text_properties,
                          .parent = TK_PARENT_VTABLE(widget),
                          .create = border_text_create,
                          .on_paint_self = border_text_on_paint_self,
                          .on_paint_border = widget_on_paint_null,
                          .on_paint_background = widget_on_paint_null,
                          .set_prop = border_text_set_prop,
                          .get_prop = border_text_get_prop,
                          .on_event = border_text_on_event,
                          .on_destroy = border_text_on_destroy};

widget_t* border_text_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
  widget_t* widget = widget_create(parent, TK_REF_VTABLE(border_text), x, y, w, h);
  border_text_t* border_text = BORDER_TEXT(widget);
  return_value_if_fail(border_text != NULL, NULL);



  return widget;
}

widget_t* border_text_cast(widget_t* widget) {
  return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, border_text), NULL);

  return widget;
}
